﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace SmartSpace.Web
{
    public static class PartUtility
    {
        public static void RegisterCss(this UserControl control, string key, string module, string relativePath)
        {
            foreach (Control item in control.Page.Header.Controls)
            {
                if (item.ID == key)
                {
                    return;
                }
            }

            string resolvedPath = relativePath;
            if (resolvedPath.StartsWith("./"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }
            HtmlLink link = new HtmlLink();
            link.ID = key;
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            link.Attributes.Add("href", control.ResolveUrl(string.Format("~/{0}/{1}", module, resolvedPath)));
            control.Page.Header.Controls.Add(link);
        }

        public static void RegisterScript(this UserControl control, string key, string module, string relativePath)
        {
            foreach (Control item in control.Page.Header.Controls)
            {
                if (item.ID == key)
                {
                    return;
                }
            }

            string resolvedPath = relativePath;
            if (resolvedPath.StartsWith("./"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }

            HtmlGenericControl script = new HtmlGenericControl("script");
            script.ID = key;
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", control.Page.ResolveUrl(string.Format("~/{0}/{1}", module, resolvedPath)));
            control.Page.Header.Controls.Add(script);
        }
    }
}
